package com.example.restatr.Atcivity.Movie;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Movie.Bean.MovieFilmBean;
import com.example.restatr.Atcivity.Movie.Bean.MovieTheatreBean;
import com.example.restatr.Atcivity.Movie.Bean.MovieTheatreTimesBean;
import com.example.restatr.Atcivity.Movie.Fragment.MovieHotFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class MovieCinemaInfoAt extends AppCompatActivity {
    private ImageView head;
    private ImageView back;
    private TextView name;
    private TextView location;
    private RatingBar score;
    private TextView level;
    private LinearLayout Null;
    private RecyclerView movieComment;
    private TextView distance;
    private List<MovieTheatreTimesBean.RowsBean> rowsBeanList = new ArrayList<>();//影院影片场次信息
    private MovieTheatreBean.RowsBean rowsBean;//影院信息Bean

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_cinema_info_at);

        head = findViewById(R.id.head);
        back = findViewById(R.id.back);
        name = findViewById(R.id.name);
        location = findViewById(R.id.location);
        score = findViewById(R.id.score);
        level = findViewById(R.id.level);
        Null = findViewById(R.id.Null);
        movieComment = findViewById(R.id.movieComment);
        distance = findViewById(R.id.distance);
        String date = getIntent().getStringExtra("date");
        rowsBean = new Gson().fromJson(date, new TypeToken<MovieTheatreBean.RowsBean>() {
        }.getType());
        Glide.with(this)
                .load(Tools.ip + rowsBean.getCover())
                .into(head);
        name.setText(rowsBean.getName());
        location.setText(rowsBean.getAddress());
        score.setRating(rowsBean.getScore());
        level.setText(String.valueOf(rowsBean.getScore()));
        distance.setText(rowsBean.getDistance());

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        GetTheatreTimes();
    }


    private void GetTheatreTimes() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/movie/theatre/times/list?theaterId=" + rowsBean.getId(), "");
            MovieTheatreTimesBean movieTheatreTimesBean = new Gson().fromJson(get,MovieTheatreTimesBean.class);
            rowsBeanList.addAll(movieTheatreTimesBean.getRows());
            if (rowsBeanList.size() > 0) {
                runOnUiThread(() -> {
                    movieComment.setAdapter(new adapter());
                });
            }
        }).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.adapterViewHolder> {

        @NonNull
        @Override
        public adapter.adapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.movie_hot_item_layout, parent, false);
            return new adapter.adapterViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull adapter.adapterViewHolder holder, int position) {
            MovieFilmBean.RowsBean rowsBean = MovieHotFragment.rowsBeanList.stream().filter(r -> r.getId() == rowsBeanList.get(position).getMovieId()).findAny().orElse(null);
            Glide.with(MovieCinemaInfoAt.this)
                    .load(Tools.ip + rowsBean.getCover())
                    .transform(new RoundedCorners(15))
                    .error(R.drawable.bj)
                    .into(holder.cover);
            holder.name.setText(rowsBean.getName());
            holder.likeAndFavorite.setText("上映时间：" + rowsBeanList.get(position).getPlayDate());
            holder.playDate.setText(rowsBeanList.get(position).getStartTime() + "--" + rowsBeanList.get(position).getEndTime()+"影厅："+rowsBeanList.get(position).getRoomName());
            holder.rating.setVisibility(View.GONE);
            holder.score.setText("票数："+(rowsBeanList.get(position).getTotalNum()-rowsBeanList.get(position).getSaleNum()));
            holder.btnText.setText(rowsBeanList.get(position).getPrice()+"¥");
            holder.score.setTypeface(Typeface.DEFAULT);
            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MovieCinemaInfoAt.this, ChooseSeatAt.class);
                    intent.putExtra("影院影片场次信息",new Gson().toJson(rowsBeanList.get(position)));
                    intent.putExtra("电影信息",new Gson().toJson(rowsBean));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeanList.size();
        }

        class adapterViewHolder extends RecyclerView.ViewHolder {
            private ImageView cover;
            private TextView name;
            private TextView likeAndFavorite;
            private TextView playDate;
            private RatingBar rating;
            private TextView score;
            private CardView btn;
            private TextView btnText;


            public adapterViewHolder(@NonNull View itemView) {
                super(itemView);
                cover = itemView.findViewById(R.id.cover);
                name = itemView.findViewById(R.id.name);
                likeAndFavorite = itemView.findViewById(R.id.likeAndFavorite);
                playDate = itemView.findViewById(R.id.playDate);
                rating = itemView.findViewById(R.id.rating);
                score = itemView.findViewById(R.id.score);
                btn = itemView.findViewById(R.id.btn);
                btnText = itemView.findViewById(R.id.btnText);
            }
        }
    }

}